package com.hetao.xx.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.cardview.widget.CardView;
import androidx.fragment.app.Fragment;

import com.hetao.xx.Global;
import com.hetao.xx.R;
import com.hetao.xx.adapter.NpcAdapter;
import com.hetao.xx.adapter.PlaceAdapter;
import com.hetao.xx.game.model.Npc;
import com.hetao.xx.game.model.Place;
import com.hetao.xx.orm.NpcVo;
import com.hetao.xx.orm.PlaceVo;
import com.hetao.xx.service.PlaceEventService;
import com.hetao.xx.service.PlaceManager;
import com.hetao.xx.utils.UtilList;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class HomePlaceFragment extends Fragment {

    private View root;
    private Unbinder unbinder;
    private PlaceVo place;

    @SuppressLint("NonConstantResourceId")
    @BindView(R.id.home_place_info_layout)
    CardView infoLayout;
    @SuppressLint("NonConstantResourceId")
    @BindView(R.id.home_place_site_layout)
    CardView siteLayout;
    @SuppressLint("NonConstantResourceId")
    @BindView(R.id.home_place_npc_layout)
    CardView npcLayout;
    @SuppressLint("NonConstantResourceId")
    @BindView(R.id.home_place_info)
    TextView infoView;
    @SuppressLint("NonConstantResourceId")
    @BindView(R.id.home_place_npc_list)
    ListView npcList;
    @SuppressLint("NonConstantResourceId")
    @BindView(R.id.home_place_site_list)
    ListView siteList;
    @SuppressLint("NonConstantResourceId")
    @BindView(R.id.home_place_site_back)
    Button backButton;

    NpcAdapter npcAdapter;
    PlaceAdapter siteAdapter;

    private final Global global = Global.getInstant();
    private final PlaceManager placeManager = global.placeManager;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (root == null) {
            root = inflater.inflate(R.layout.fragment_home_place, container, false);
            unbinder = ButterKnife.bind(this, root);
            init();
        }
        return root;
    }

    public void init() {
        siteAdapter = new PlaceAdapter(getContext(), new ArrayList<>(4));
        siteList.setAdapter(siteAdapter);
        siteAdapter.setOnClickListener(v -> {
            PlaceVo place = (PlaceVo)v.getTag();
            if (place != null) {
                PlaceEventService.clickPlace(place);
            }
        });

        npcAdapter = new NpcAdapter(getContext(), new ArrayList<>(4));
        npcList.setAdapter(npcAdapter);
        npcAdapter.setOnClickListener(v -> {
            NpcVo npc = (NpcVo)v.getTag();
            if (npc != null) {
                PlaceEventService.clickNpc(npc);
            }
        });

    }

    public void update() {
        if(TextUtils.isEmpty(place.message)) {
            infoLayout.setVisibility(View.GONE);
        } else {
            infoLayout.setVisibility(View.VISIBLE);
            infoView.setText(place.message);
        }

        List<PlaceVo> siteList = placeManager.getChildrenById(place.id);
        if(UtilList.isEmpty(siteList)) {
            siteLayout.setVisibility(View.GONE);
        } else {
            siteLayout.setVisibility(View.VISIBLE);
            siteAdapter.setList(siteList);
            siteAdapter.notifyDataSetChanged();
        }

        List<NpcVo> npcList = placeManager.getNpcByPlaceId(place.id);
        if(UtilList.isEmpty(npcList)) {
            npcLayout.setVisibility(View.GONE);
        } else {
            npcLayout.setVisibility(View.VISIBLE);
            npcAdapter.setList(npcList);
            npcAdapter.notifyDataSetChanged();
        }


        backButton.setVisibility(place.prePlaceId == 0 ? View.GONE : View.VISIBLE);

    }

    @SuppressLint("NonConstantResourceId")
    @OnClick(R.id.home_place_site_back)
    public void onClick(View view) {
        PlaceEventService.backPlace(place);
    }

    public void setPlaces(PlaceVo place) {
        this.place = place;
    }

    @Override
    public void onResume() {
        super.onResume();
        update();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        //进行判空，避免空指针
        if (unbinder != null) {
            unbinder.unbind();
        }
    }
}
